<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/15
 * Time: 15:18
 */
namespace app\api\controller;
class Uploads{
    public $filterExt=array("php","sh");
    public function  uploadFiles()
    {
        $auth =trim(input("request.auth"));//用户auth
        $key =trim(input("request.key"));//用户auth
        $type =trim(input("request.type"));//文件类型
        $userInfo = checkLoginExp($auth);
        if(empty($userInfo))
        {
            $return = ['err'=>1,'msg'=>'获取失败,未登录','devMsg'=>'获取失败，参数错误[auth]','key'=>$key];
            return json($return);
        }
        $scenePic = request()->file('file');
        $path = generaUploadPath($userInfo['id']);
        $resultData = uploadPicture($scenePic,$path);
        $pic='';
        if($resultData['err'] =='0')
        {
            $pic = $resultData['file']['url'];
        }
        $pic = str_replace("\\","/",$pic);
        $iData=array(
            'url'=>$pic,
            'pic'=>$pic,
            'thumb'=>""
        );

        if(getSuffixForFile($pic)=="php")
        {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            echo json_encode($return, true);exit;
        }
        if($pic)
        {
            $return['err']=0;
            $return['msg']='成功';
            $return['key']=$key;
            $return['type']=$type;
            $return['data']=$iData;
            return json($return);
        }
        else
        {
            $return['err']=10009;
            $return['key']=$key;
            $return['msg']='失败';
            return json($return);
        }
    }

    private function ossFileName($file)
    {
        $filename = $file['name'];
        $arr = explode('.', $filename);
        $ext = end($arr);  //end()
        $temp = autoGerCode();
        $newFile = 'images' . '/' . date("Y-m-d") . '/' . $temp;
        if ($ext) {
            $newFile = $newFile . "." . $ext;
        }
        return $newFile;
    }
    public function ossUpload()
    {
        $key =trim(input("request.key"));//用户auth
        $type =trim(input("request.type"));//文件类型
        $is_thumb = trim($_REQUEST['is_thumb']);
        $currDate = getCurrDate();
        $scenePic = $_FILES['file'];
        $ossPath = $this->ossFileName($scenePic);
        $localPath =$scenePic['tmp_name'];
        $ossResult = ossUpload($localPath,$ossPath);
        if ($ossResult['info']['url']) {
            $fileModel = new \app\admin\model\Files();
            $insertID = $fileModel->add(array(
                'file' => $ossResult['info']['url'],
                'date' => date("Y-m-d"),
                'ctime' => time(),
                'name' => $scenePic['name'],
                'ty' => 2,
                'types'=>$ossResult['oss-requestheaders']['Content-Type']
            ));
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] =  array(
                'pic' =>$ossResult['info']['url'],
                'thumb' => $ossResult['info']['url'],
            );
            $return['key']=$key;
            $return['type']=$type;
            echo json_encode($return, true);
        } else {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            $return['key']=$key;
            $return['type']=$type;
            echo json_encode($return, true);
        }
    }



    /**
     * 多文件上传 返回总的上传文件数量 成功的文件数量 失败的文件数量 和成功的文件地址
     * @return \think\response\Json
     */
    public function  mUploadFiles()
    {
        $auth =trim(input("request.auth"));//用户auth
        $key =trim(input("request.key"));//用户auth
        $userInfo = checkLoginExp($auth);
        if(empty($userInfo))
        {
            $return = ['err'=>1,'msg'=>'获取失败,未登录','devMsg'=>'获取失败，参数错误[auth]','key'=>$key];
            return json($return);
        }
        $scenePic = request()->file('file');
        $totleFileNum = count($scenePic);//总的文件数量
        $failFileNum = 0;//失败的文件数量
        $successFileNum = 0;//成功的文件数量
        $returnFileData=array();
        if($totleFileNum<=0)
        {
            $return['err']=10009;
            $return['key']=$key;
            $return['totleFileNum']=$totleFileNum;
            $return['failFileNum']=$failFileNum;
            $return['successFileNum']=$successFileNum;
            $return['data']=[];
            $return['msg']='上传失败，请选择上传文件';
            return json($return);
        }
        for($i=0;$i<$totleFileNum;$i++)
        {
            $path = generaUploadPath($userInfo['id']);
            $resultData = uploadPicture($scenePic[$i],$path);
            $pic='';
            if($resultData['err'] =='0')
            {
                $successFileNum++;
                $pic = $resultData['file']['url'];
                $pic = str_replace("\\","/",$pic);
                if(getSuffixForFile($pic)=="php")
                {
                    $return['err'] = 10009;
                    $return['msg'] = '失败';
                    echo json_encode($return, true);exit;
                }
                $iData=array(
                    'url'=>$pic,
                    'pic'=>$pic,
                    'thumb'=>""
                );
                $returnFileData[]=$iData;
            }else
            {
                $failFileNum++;
            }

        }

            $return['err']=0;
            $return['msg']='操作成功';
            $return['key']=$key;
            $return['totleFileNum']=$totleFileNum;
            $return['failFileNum']=$failFileNum;
            $return['successFileNum']=$successFileNum;
            $return['data']=$returnFileData;
            return json($return);

    }




    public function  wechatAppUploadFiles()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
      
        $userInfo = getUserinfoForAccessToken($access_token,1,$_hosp);
        if (empty($userInfo)) {
            $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
            if(empty($userInfo))
            {
                $return['err'] = 10009;
                $return['msg'] = '未找到相关用户';
                echo json_encode($return, true);
                exit;
            }
        }
        $key =trim(input("request.key"));//用户auth
        $scenePic = request()->file('file');
        $oldFileName = $scenePic->getInfo();
        $path = generaUploadPath($userInfo['id']);
        $resultData = uploadPicture($scenePic,$path);
        $pic='';
        if($resultData['err'] =='0')
        {
            $pic = $resultData['file']['url'];
        }
        $pic = str_replace("\\","/",$pic);
        $iData=array(
            'url'=>$pic,
            'pic'=>$pic,
            'thumb'=>"",
            'name'=>$oldFileName['name']
        );
        if(getSuffixForFile($pic)=="php")
        {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            echo json_encode($return, true);exit;
        }
        if($pic)
        {
            $return['err']=0;
            $return['msg']='成功';
            $return['key']=$key;
            $return['data']=$iData;
            return json($return);
        }
        else
        {
            $return['err']=10009;
            $return['key']=$key;
            $return['msg']='失败';
            return json($return);
        }
    }

    public function cosUpload()
    {
        $res = cosUpload();
        print_r($res);
    }
    public function aaa(){
        $local_file = '/wwwroot/mysite/a.jpg';//本地文件
        $server_key_name = '/upload/20191118/a.jpg';//远程文件名
        uploadToTencentyun_new($local_file , $server_key_name , true);
    }
}